

package com.transpeed.payservice.webDaoImpl;

import java.io.IOException;

import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.json.JSONObject;

import com.google.gson.Gson;
import com.transpeed.payservice.Utils.JsonUtils;
import com.transpeed.payservice.log.InitLogger;
import com.transpeed.payservice.log.Log;
import com.transpeed.payservice.model.UmsPayCarInfo;
import com.transpeed.payservice.model.UmsPayNotifyInfo;
import com.transpeed.payservice.pay.unionpay.UmsPayController;


/**
 * TODO 银联二维码相关API
 * GetUmsPayQRCode 获取银联C扫B二维码
 * QueryUmsPayTrade 查询银联订单
 * @author lizhi
 * @date 2017年4月10日
 * 更新日志
 * 2017年4月10日 lizhi 首次创建
 *
 */
@Path("/UmsPayAPI")
public class UmsPayAPI {
	
	@POST
	@Path("/GetUmsPayQRCode")
	@Produces(MediaType.TEXT_HTML)
	// @Produces(MediaType.APPLICATION_JSON)
	// @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	// 本地测试
	// http://192.168.0.102:8080/TranSpeed-PayService/UmsPayAPI/GetUmsPayQRCode
	// 服务器测试
	// http://122.97.128.110:8080/TranSpeed-PayService/UmsPayAPI/GetUmsPayQRCode
	// 物联网测试
	// http://192.168.254.5:8080/TranSpeed-PayService/UmsPayAPI/GetUmsPayQRCode
	
	public String GetUmsPayQRCode(
			@FormParam("TotalAmount") String TotalAmount,
			@FormParam("CarInPlace") String CarInPlace,
			@FormParam("CarOutPlace") String CarOutPlace,
			@FormParam("CarPlate") String CarPlate,
			@FormParam("CarInTime") String CarInTime,
			@FormParam("url") String NotifyUrl

	) throws IOException {

		UmsPayCarInfo ParkInfo = new UmsPayCarInfo();
		ParkInfo.setTotalAmount(TotalAmount);
		ParkInfo.setCarInPlace(CarInPlace);
		ParkInfo.setCarOutPlace(CarOutPlace);
		ParkInfo.setCarPlate(CarPlate);
		ParkInfo.setCarInTime(CarInTime);
		ParkInfo.setNotifyUrl(NotifyUrl);

		InitLogger.Createfolder();

		Log.Write("收到上位机生成银联二维码请求！", "UmsPay");
		Log.Write("收到上位机车辆信息为：\n" + JsonUtils.formatAsJSON(new JSONObject(ParkInfo).toString()), "UmsPay");
		Log.Write("开始向银联传送生成二维码请求！", "UmsPay");
		String url = UmsPayController.getUnionQRCode(TotalAmount ,CarPlate , CarInPlace);	
		Log.Write("二维码生成成功！已推送给上位机!返回信息为：" + url, "UmsPay");

		return url;
	}

	/**
	 * @author 用于接收上位机查询银联订单请求
	 * 
	 */
	
	@POST
	@Path("/QueryUmsPayTrade")
	@Produces(MediaType.TEXT_HTML)
	// @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	// 本地测试
	// http://192.168.0.102:8080/TranSpeed-PayService/UmsPayAPI/QueryUmsPayTrade
	// 服务器测试
	// http://122.97.128.110:8080/TranSpeed-PayService/UmsPayAPI/QueryUmsPayTrade
	// 物联网测试服务器
	// http://192.168.254.5:8080/TranSpeed-PayService/UmsPayAPI/QueryUmsPayTrade
	public String QueryUmsPayTrade(
			@FormParam("TradeNumber") String TradeNumber,
			@FormParam("TradeDate") String TradeDate)
			throws IOException {

		Log.Write("收到上位机查询银联二维码订单状态请求！ 订单号为：" + TradeNumber, "UmsPay");
		String TradeStatus = UmsPayController.queryUnionTrade(TradeNumber, TradeDate);
		Log.Write("已通知上位机！订单查询结果为：" + TradeStatus, "UmsPay");
		return TradeStatus;
	}

	/**
	 * @author lizhi 用于接收用户付款成功后银联回传信息
	 */

	@POST
	@Path("/ReceiveUmsPayTradeInfo")
	@Produces(MediaType.TEXT_HTML)
	//@Produces(MediaType.APPLICATION_JSON)
	// @Consumes(MediaType.APPLICATION_JSON)
	// 访问方法路径
	// http://112.81.110.151:4000/TranSpeed-PayService/UmsPayAPI/ReceiveUmsPayTradeInfo
	public String ReceiveUmsPayTradeInfo(
			@FormParam("mid") String mid,
			@FormParam("tid") String tid,
			@FormParam("instMid") String instMid,
			@FormParam("billNo") String billNo,
			@FormParam("billQRCode") String billQRCode,
			@FormParam("billDate") String billDate,
			@FormParam("billStatus") String billStatus,
			@FormParam("memberId") String memberId,
			@FormParam("counterNo") String counterNo,
			@FormParam("billPayment") String billPayment		
			) throws IOException {
		
		Gson gson = new Gson();
		UmsPayNotifyInfo info = gson.fromJson(billPayment , UmsPayNotifyInfo.class);
		
		info.setMid(mid);
        info.setTid(tid);
        info.setInstMid(instMid);
        info.setBillNo(billNo);
        info.setBillQRCode(billQRCode);
        info.setBillDate(billDate);
        info.setBillStatus(billStatus);
        info.setMemberId(memberId);
        info.setCounterNo(counterNo);
        
        Log.Write("收到银联推送信息为：\n" + JsonUtils.formatAsJSON(new JSONObject(info).toString()), "ReceiveUmsPay");
		System.out.println(billPayment);
		return mid;
	}

}
